import store from '../store'
import before from './before'
import { createRouter, createWebHashHistory } from 'vue-router';

//本页为路由核心部分
const routes = createRoutesFromFiles(require.context('../', true, /router\.js/))

function createRoutesFromFiles(routeFiles) {
    let _routes = [ //公共页面配置
        {
            path: "/",
            name: 'login',
            component: () => import('@/view/login/index.vue')
        },
    ]

    // 循环获取router配置
    routeFiles.keys().forEach(key => {
        //排除自身
        if (key === './router/index.js') return
        let keyList = key.split('/')
        if (keyList[keyList.length - 1] !== 'router.js') return

        let item = routeFiles(key).default  //读取文档中的default模块
        //数组对象循环添加
        if (item instanceof Array) item.forEach(route => _routes.push(route))
        else _routes.push(item)
    })

    return _routes
}

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

//跳转拦截
before(router, store)

export default router
